// 窗口对话 -- 轻提示
import { $copy,$toClass } from "./main";
// 窗口对话 -- 轻提示
/* 
    依赖 vant
    使用方法
    $log(opts:Object|String[,ass:Number|Object|Function])
    $log("你好");
    $log("Nihao",1500); // 1500毫秒关闭
    $log("nihao",()=>{
        // layer关闭后的回调函数
    });
    $log("nihao",{ //layer其他配置参数 })；
    或者
    $log({ //layer其他配置参数 })
*/

function $log(opts, ass) {
	return new Promise((resolve) => {
		if (opts) {
			let defaultOpts = {
				duration: 2000
			};
			let optsType = $toClass(opts);
			if (optsType == "String") {
				defaultOpts.message = opts;
			}
			else if (optsType == "Object") {
				$copy(defaultOpts, opts);
			}
			else {
				throw new TypeError("the opts must be String ");
			}
			let endFun;
			if (ass) {
				let assType = $toClass(ass);
				switch (assType) {
					case "Number":
						defaultOpts.duration = ass; break;
					case "Function":
						endFun = ass; break;
					case "Object":
						$copy(defaultOpts, ass); break;
					default:
						throw new TypeError("the opts must be Number Object or Function");

				}
			}
			let { message } = defaultOpts;
			if (message) {
				this.$toast(
					$copy(defaultOpts, {
						onClose() {
							endFun && endFun();
							resolve();
						}
					})
				);
			} else {
				throw new ReferenceError("the options message is must be required");
			}
		} else {
			throw new ReferenceError("the opts must be required");
		}
	});
}
// 对话框原型
/* 
    $dialog(opts:String:Object,type:Object);
*/
function $dialog(opts, type) {
	return new Promise((resolve, reject) => {
		if (opts) {
			let defaultOpts = {};
			let optsType = $toClass(opts);
			if (optsType == "String") {
				defaultOpts.message = opts;
			} else if (optsType == "Object") {
				$copy(defaultOpts, opts);
			} else {
				throw new TypeError("the opts type must be String or Object");
			}
			this.Dialog[`$${type}`](defaultOpts)
				.then(() => {
					resolve();
				}).catch(() => {
					reject();
				});

		} else {
			throw new ReferenceError("the opts is must required");
		}
	});
}
// alert 对话框
/* 
	$alert(opts:String|Object)
*/
function $alert(opts) {
	return $dialog(opts, "alert");
}
// confrim 对话框
/* 
	$confirm(opts:String|Object)
*/
function $confirm(opts) {
	return $dialog(opts, "confirm");
}
// 失败提示
/* 
	$fail(err:String,reak:Boolean)
*/
function $fail(err, real = false) {
	console.log(err);
	this.$log(real ? err : "请求失败！");
}

export  {
    $log
    ,$alert
    ,$confirm
    ,$fail
}